/*
 * driver.java
 *
 * Created on May 17, 2005, 3:44 PM
 */

package aa.drivers;

import aa.core.*;
import aa.gui.View;
import aa.tool.CyberOrgTuple;

import java.net.InetAddress;
import java.util.LinkedList;
import java.util.Hashtable;
import java.io.*;
import java.sql.Driver;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author  xinghuizhao
 */
public class driver{

    
    /** Creates a new instance of driver */
    public driver() {
    }
    
    private static void testReceiveCyberOrg(Platform p_pPlatform){
        //ActorName remote= new ActorName("128.233.109.164",2);
        ActorName remote= new ActorName("192.168.1.100",2);
        Object[] obja={new Contract(),
                       new Long(0),
                       
                       new LinkedList()};
        
        ActorMessage msg = new ActorMessage(p_pPlatform.getActorNameOfPlatform(), 
                                            remote, 
                                            "receiveCyberOrg", 
                                            obja,
                                            false);
        p_pPlatform.sendMessage(msg);
        
    }
    
    public static void testHash(){
        ActorName an1= new ActorName("128.233.109.239",0);
        ActorName an2 = new ActorName("128.233.109.239",0);
        //an1.setLocationAddress("128.109.233.209");
        
        String s1="asdf";
        String s2="asdf";
        
        Hashtable hash=new Hashtable();
        hash.put(s1,"ejk");
        System.out.println(an1+" "+an2);
        System.out.println(hash.get(s2));
        
        System.out.println(hash.containsKey(an2));
        if (an1.equals(an2)){
            System.out.println("Equallllll");
        }
        
    }

    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        //testHash();

        final String brokerHost = "10.225.160.74";//"192.168.1.105";

        Integer iPort = new Integer(8070);
        
        View myView = new View(iPort);
        
        Platform myPlatform = new Platform(myView,iPort);
        
        myPlatform.start();
        
        Object[] objaAgs = new Object[0];
        
        Object[] objaFaci = {new Long(0)};
        
        
        //testReceiveCyberOrg(myPlatform);
        
        CyberOrg cyb = (CyberOrg)myPlatform.createCyberOrg(new Long(90000000),new Long(90000),new Long(1000),"aa.core.FacilitatorActor",objaAgs);
        
        ActorName broker = new ActorName(brokerHost, 5);
        CyberOrgTuple cyberOrgTuple=new CyberOrgTuple(myPlatform.getActorNameOfPlatform().getHostAddress(), 2, "aa.core.FacilitatorActor", new Contract(1000000,1000,0));
        Object[] argsreg=new Object[1];
        argsreg[0]=cyberOrgTuple;
        

        ActorMessage am_reg = new ActorMessage(myPlatform.getActorNameOfPlatform(),
                                               broker,
                                               "registerToCDM",
                                               argsreg,
                                               false
                                               );

        myPlatform.sendMessage(am_reg);

//
//        argsreg[0]= new Contract(10000,100,1);
//        ActorMessage am_getlist = new ActorMessage(myPlatform.getActorNameOfPlatform(),
//                                               broker,
//                                               "getPotentialResourceList",
//                                               argsreg,
//                                               false
//                                               );
//
//        myPlatform.sendMessage(am_getlist);

        

        Object[] objaAgsCreate = new Object[0];
        
        
        
        
        ActorName actor1 = myPlatform.createActor(cyb.getFacilitator(), "aa.application.Fibonacci.FibActor", objaAgsCreate);
        
        ActorName[] actors1 = new ActorName[1];
        actors1[0]=actor1;
        
        
        CyberOrg child = cyb.isolate(10, actors1, new Contract(20000000,20000,0));
        
        
        System.out.print("Please input the IP address of the destination node: ");
        
        String remoteDesti=brokerHost;
        System.out.println(remoteDesti);
        ActorName remoteFaci = new ActorName(remoteDesti,2);
        
        
        
        System.out.println("before migration: " + System.currentTimeMillis());

//        ActorName driver=null;
//        try {
//            driver = myPlatform.createActor("app.quickstart.sum.Add", new Object[0]);
//            Thread.sleep(1000);
//
//            Object[] Args=new Object[3];
//            Args[0] = remoteFaci;
//            Args[1] = "negotiate";
//            Args[2] = (Object)new Contract(100000000,1000,0);
//            ActorMessage am_negotiate = new ActorMessage(myPlatform.getActorNameOfPlatform(),
//                                               driver,
//                                               "callNegotiate",
//                                               Args,
//                                               false
//                                               );
//            myPlatform.sendMessage(am_negotiate);
//            Thread.sleep(10000);
//
//
//
//        }
//        catch (InterruptedException ex) {
//            Logger.getLogger(driver.class.getName()).log(Level.SEVERE, null, ex);
//        }catch (CreateActorException ex) {
//            ex.printStackTrace();
//        }
        
        child.migrate(remoteFaci, new Contract(100000000,1000,0));
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException ex) {
//            Logger.getLogger(driver.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        ActorName fibonacciActor=(ActorName)child.getActors().get(1);
//        int fibNum = 4;
//        Object[] Ags=new Object[3];
//        Ags[0]=new Integer(fibNum);
//        Ags[1]=fibonacciActor;
//        Ags[2]="result";
//
//        ActorMessage am_fib = new ActorMessage(myPlatform.getActorNameOfPlatform(),
//                                               fibonacciActor,
//                                               "fib",
//                                               Ags,
//                                               false
//                                               );
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException ex) {
//            Logger.getLogger(driver.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        myPlatform.sendMessage(am_fib);
//        try {
//
//            Object[] Args=new Object[2];
//            Args[0] = remoteDesti;
//            Args[1] = "returnAllTuples";
//            ActorMessage am_returnAll = new ActorMessage(myPlatform.getActorNameOfPlatform(),
//                                               driver,
//                                               "sendMessage",
//                                               Args,
//                                               false
//                                               );
//            myPlatform.sendMessage(am_returnAll);
//            Thread.sleep(10000);
//
//        }
//        catch(InterruptedException ie){
//            ie.printStackTrace();
//        }
        

        //d.sendMessage(anCybDM, "returnAllTuples");
        /*Object[] objaIso = new Object[3];
        objaIso[0] = new Long(2);
        objaIso[1] = actors21;
        objaIso[2] = new Contract(5,5,1);
       
        
        Object[] emptyObja = {};
        ActorMessage am_iso = new ActorMessage(myPlatform.getActorNameOfPlatform(),
                                               child21.getFacilitator(), 
                                               "remoteControlIsolate", 
                                               objaIso, 
                                               false
                                               );
        //myPlatform.sendMessage(am_iso);
        
        ActorMessage am_assi = new ActorMessage(myPlatform.getActorNameOfPlatform(),
                                               child21.getFacilitator(), 
                                               "remoteControlAssimilate", 
                                               emptyObja, 
                                               false
                                               );*/
        //myPlatform.sendMessage(am_assi);
        
    }

   
    
}
